package com.wxxymaker.utils.web;

import java.util.Collection;

public class ResponseWarp<T> {
    // 返回码
    private Integer code;
    // 返回消息
    private String  msg;
    // 是否成功
    private Boolean success;
    // 返回数据
    private T       data;
    // 返回数据长度
    private Integer length = 1;

    private ResponseWarp() { }

    public ResponseWarp(Integer code, String msg, boolean success, T data) {
        this.code = code;
        this.msg = msg;
        this.success = success;
        this.data = data;

        if (data == null) {
            this.length = 0;
        } else if (data instanceof Collection) {
            this.length = ((Collection) data).size();
        }
    }

    public static <T> ResponseWarp<T> ok(){
        return new ResponseWarp<>(200, null, true, null);
    }

    public static <T> ResponseWarp<T> ok(String msg) {
        return new ResponseWarp<>(200, msg, true, null);
    }

    public static <T> ResponseWarp<T> ok(T data) {
        return new ResponseWarp<>(200, null, true, data);
    }

    public static <T> ResponseWarp<T> ok(T data, String msg) {
        return new ResponseWarp<>(200, msg, true, data);
    }

    public static <T> ResponseWarp<T> error() {
        return new ResponseWarp<>(404, "Not Found", false, null);
    }

    public static <T> ResponseWarp<T> error(Integer code) {
        return new ResponseWarp<>(code, null, false, null);
    }

    public static <T> ResponseWarp<T> error(String msg) {
        return new ResponseWarp<>(404, msg, false, null);
    }

    public static <T> ResponseWarp<T> error(Integer code, String msg) {
        return new ResponseWarp<>(code, msg, false, null);
    }


    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Boolean getSuccess() {
        return success;
    }

    public void setSuccess(Boolean success) {
        this.success = success;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Integer getLength() {
        return length;
    }

    public ResponseWarp setLength(Integer length) {
        this.length = length;
        return this;
    }
}
